unit untCadastroFornecedor;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, untCadastroPadrao, FMTBcd, DB, SqlExpr, Buttons, ExtCtrls,
  StdCtrls, Mask, DBCtrls;

type
  TfrmCadastroFornecedor = class(TfrmCadastroPadrao)
    edtcodigo: TEdit;
    edtnomefant: TEdit;
    edtsite: TEdit;
    edtobs: TEdit;
    edtrazaosocial: TEdit;
    edtinscestadual: TEdit;
    cbbsituacao: TComboBox;
    dblestado: TDBLookupComboBox;
    dblmunicipio: TDBLookupComboBox;
    edtcnpj: TMaskEdit;
    edtfone: TMaskEdit;
    edtemail: TEdit;
    edtendereco: TEdit;
    edtcep: TMaskEdit;
    edtdatacad: TMaskEdit;
    lbl1: TLabel;
    lbl2: TLabel;
    lbl3: TLabel;
    lbl4: TLabel;
    lbl5: TLabel;
    lbl6: TLabel;
    lbl7: TLabel;
    lbl8: TLabel;
    lbl9: TLabel;
    lbl10: TLabel;
    lbl11: TLabel;
    lbl12: TLabel;
    lbl13: TLabel;
    lbl14: TLabel;
    lbl15: TLabel;
    procedure dblestadoClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnGravarClick(Sender: TObject);
    procedure LimparCampos();
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCadastroFornecedor: TfrmCadastroFornecedor;

implementation

uses untCadastroCliente, untDM, untFuncao;

{$R *.dfm}

procedure TfrmCadastroFornecedor.LimparCampos();
var  i : integer;
begin
  for  i := 0  to ComponentCount -1 do
  begin
    if (Components[i] is TEdit) Then
       (Components[i] as TEdit).Clear;

    if (Components[i] is TMaskEdit) then
       (Components[i] as TMaskEdit).Clear;

    if (Components[i] is TComboBox) then
       (Components[i] as TComboBox).ItemIndex := -1;

    if (Components[i] is TDBLookupComboBox) then
       (Components[i] as TDbLookupComboBox).KeyValue := null;
  end;

end;

procedure TfrmCadastroFornecedor.dblestadoClick(Sender: TObject);
begin
  inherited;
  DM.sdsMunicipio.Active := False;
  DM.sdsMunicipio.DataSet.ParamByName('COD_UF_IBGE').AsInteger := strtoint(dblestado.KeyValue);
  dblmunicipio.ListField := 'MUNICIPIO';
  dblmunicipio.KeyField := 'COD_MUN_IBGE';
  DM.sdsMunicipio.Active := true;
end;

procedure TfrmCadastroFornecedor.FormShow(Sender: TObject);
begin
  inherited;
  dm.cdsFornecedor.Open;
  DM.sdsEstado.Active := True;
  DM.sdsMunicipio.Active := True;
  if Self.Tag = 1 then
  begin
    Caption := 'Cadastrar Fornecedor';
    edtdatacad.Text := DateToStr(Date);
  end
  else
  if Self.Tag = 2 then
  begin
    Caption := 'Alterar Fornecedor';

    with dm.cdsFornecedor do
    begin
      edtcodigo.Text := FieldByName('for_codigo').AsString;
      cbbsituacao.ItemIndex := cbbsituacao.Items.IndexOf(FieldByName('for_situacao').AsString);
      edtnomefant.Text := FieldByName('for_nomefant').AsString;
      edtcnpj.Text := fieldByName('for_cnpj').AsString;
      edtinscestadual.Text := fieldByName('for_inscestadual').AsString;
      edtrazaosocial.Text := fieldByName('for_razaosocial').AsString;
      edtfone.Text := fieldByname('for_fone').AsString;
      edtdatacad.Text := fieldByName('for_datacad').AsString;
      edtendereco.Text := fieldByName('for_endereco').AsString;
      dblestado.KeyValue := fieldByName('codigo_uf_ibge').AsInteger;
        if dblestado.Text <> '' then
        begin
          DM.sdsMunicipio.Active := False;
          DM.sdsMunicipio.DataSet.ParamByName('COD_UF_IBGE').AsInteger := strtoint(dblestado.KeyValue);
          dblmunicipio.ListField := 'MUNICIPIO';
          dblmunicipio.KeyField := 'COD_MUN_IBGE';
          DM.sdsMunicipio.Active := true;
        end;
      dblmunicipio.KeyValue := fieldByName('cod_mun_ibge').AsInteger;
      edtcep.Text := FieldByName('for_cep').AsString;
      edtemail.Text := FieldByName('for_email').AsString;
      edtobs.Text := fieldByName('for_obs').AsString;
      edtsite.Text := fieldByName('for_site').AsString;
    end;
  end;
end;

procedure TfrmCadastroFornecedor.btnGravarClick(Sender: TObject);
begin
  inherited;
  if Self.Tag = 1 then   // INSERIR
    sdsComando.CommandText := 'insert into FORNECEDORES (CODIGO_UF_IBGE, COD_MUN_IBGE,'+
                              'FOR_NOMEFANT, FOR_CNPJ, FOR_RAZAOSOCIAL, FOR_INSCESTADUAL,'+
                              'FOR_FONE, FOR_EMAIL, FOR_SITUACAO, FOR_SITE, FOR_OBS,'+
                              'FOR_ENDERECO, FOR_CEP, FOR_DATACAD) values (:ESTADO, :MUNICIPIO,'+
                              ':NOMEFANT, :CNPJ, :RAZAOSOCIAL, :INSCESTADUAL, :FONE, :EMAIL, '+
                              ':SITUACAO, :SITE, :OBS, :ENDERECO, :CEP, :DATACAD)'
  else
  if Self.Tag = 2 then   // UP
  begin
    sdsComando.CommandText := 'update FORNECEDORES set CODIGO_UF_IBGE = :ESTADO,'+
                              'COD_MUN_IBGE = :MUNICIPIO, FOR_NOMEFANT = :NOMEFANT,'+
                              'FOR_CNPJ = :CNPJ, FOR_RAZAOSOCIAL = :RAZAOSOCIAL,'+
                              'FOR_INSCESTADUAL = :INSCESTADUAL, FOR_FONE = :FONE,'+
                              'FOR_EMAIL = :EMAIL, FOR_SITUACAO = :SITUACAO,'+
                              'FOR_SITE = :SITE, FOR_OBS = :OBS, FOR_ENDERECO = :ENDERECO,'+
                              'FOR_CEP = :CEP, FOR_DATACAD = :DATACAD'+
                              'where FOR_CODIGO = :CODIGO';
    sdsComando.ParamByName('codigo').AsInteger := dm.cdsFornecedor.FieldByName('FOR_CODIGO').AsInteger;
  end;

  sdsComando.ParamByName('ESTADO').AsInteger := dblestado.KeyValue;
  sdsComando.ParamByName('MUNICIPIO').AsInteger := dblmunicipio.KeyValue;
  sdsComando.ParamByName('NOMEFANT').AsString := edtnomefant.Text;
  sdsComando.ParamByName('CNPJ').AsString := edtcnpj.Text;
  sdscomando.ParamByName('RAZAOSOCIAL').AsString := edtrazaosocial.text;
  sdsComando.ParamByName('INSCESTADUAL').AsString := edtinscestadual.Text;
  sdsComando.ParamByName('FONE').AsString := edtfone.Text;
  sdsComando.ParamByName('EMAIL').AsString := edtemail.Text;
  sdsComando.ParamByName('SITUACAO').AsString := cbbsituacao.Text;
  sdsComando.ParamByName('SITE').AsString := edtsite.Text;
  sdsComando.ParamByName('OBS').AsString := edtobs.Text;
  sdsComando.ParamByName('ENDERECO').AsString := edtendereco.Text;
  sdsComando.ParamByName('CEP').AsString := edtcep.Text;
  sdsComando.ParamByName('DATACAD').AsDate := StrToDate(edtdatacad.Text);
  sdsComando.ExecSQL();

  DM.cdsFornecedor.Close;
  DM.cdsFornecedor.Open;

  if Self.Tag = 1 then
  begin
    LimparCampos;
    edtnomefant.SetFocus;
  end
  else
  if Self.Tag = 2 then
    exit;


end;

end.
